#r "System.Text.Json"
#r "System.Net.Http"

using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text.Json;
using System.Collections.Generic;
using System.Threading.Tasks;

await ExtractFluentUIIcons();

async Task ExtractFluentUIIcons()
{
    var urls = new[]
    {
        "https://raw.githubusercontent.com/microsoft/fluentui-system-icons/refs/heads/main/fonts/FluentSystemIcons-Regular.json",
        "https://raw.githubusercontent.com/microsoft/fluentui-system-icons/refs/heads/main/fonts/FluentSystemIcons-Filled.json"
    };
    var outputFile = "FluentUIIcons.cs";

    var allValues = new List<KeyValuePair<string, int>>();
    var client = new HttpClient();
    foreach ( var url in urls )
    {
        var json = await client.GetStringAsync( url );
        var values = JsonSerializer.Deserialize<Dictionary<string, int>>( json );
        allValues.AddRange( values );
    }

    var names = ( from v in allValues
                  where v.Key.Contains( "_20_" )
                  select new
                  {
                      OriginalName = v.Key,
                      DisplayName = v.Key.Replace( "ic_fluent_", "" ).Replace( "_20_regular", "" ).Replace( "_20_filled", "_filled" )
                  } ).ToList();

    var result = ( from n in names
                   select new
                   {
                       DisplayName = ToPascal( n.DisplayName ),
                       n.OriginalName,
                   } )
                   .ToList();


    string header = """
    namespace Blazorise.Icons.FluentUI;

    /// <summary>
    /// Generated by csx script.
    /// Strongly-typed list of FluentUI icon names. (v1.1.227)
    /// </summary>
    public static class FluentUIIcons
    {
    """;

    var constants = result.OrderBy( x => x.DisplayName )
        .Select( x => $"    public const string {x.DisplayName} = \"icon-{x.OriginalName}\";" );

    File.WriteAllLines( outputFile, [header, .. constants, "}"] );

    Console.WriteLine( $"Exported {result.Count} FluentUI icons to {outputFile}" );
}

string GetDisplayName( string baseName, bool filled )
{
    var pascal = ToPascal( baseName );
    return filled ? $"{pascal}Filled" : pascal;
}

string ToPascal( string s ) =>
    CultureInfo.InvariantCulture.TextInfo.ToTitleCase( s.Replace( "_", " " ) ).Replace( " ", "" );
